package com.yksj.healthtalk.ui.views;

import org.universalimageloader.core.DefaultConfigurationFactory;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.app.HTalkApplication;
import com.yksj.healthtalk.utils.AnimationUtils;
/**
 * 悬浮框
 * @author jack_tang
 */
public class SuspensionFrame extends ImageView{
	private float x;
	private float y;
	private WindowManager.LayoutParams wmParams;
	private WindowManager wm;
	private float mTouchStartX;
	private float mTouchStartY;
	public String className;//当前所在class
	private DisplayMetrics displayMetrics;
	public SuspensionFrame(final Context context) {
		super(context);
		wmParams = new WindowManager.LayoutParams(); 
		wm = (WindowManager) context.getApplicationContext().getSystemService("window");
		displayMetrics = new DisplayMetrics();
	WindowManager manager=(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
	manager.getDefaultDisplay().getMetrics(displayMetrics);
		setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				getBackground().setAlpha(255);
				handler.removeMessages(1000);
				handler.sendEmptyMessageDelayed(1000, 2000);
				AnimationUtils.startGuiPager(context,className);
			}
		});
		
		setOnLongClickListener(new OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				return true;
			}
		});
		setOnTouchListener(new OnTouchListener() {
			
			private float actionUpx;
			private float actionUpy;

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// 获取相对屏幕的坐标，即以屏幕左上角为原点
				x = event.getRawX();
				// 25是系统状态栏的高度,也可以通过方法得到准确的值，自己微调就是了
				y = event.getRawY() - 20;
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					// 获取相对View的坐标，即以此View左上角为原点
					mTouchStartX = event.getX();
					mTouchStartY = event.getY() + getHeight() / 2;
					actionUpx = event.getX();
					actionUpy = event.getY();
					break;

				case MotionEvent.ACTION_MOVE:
					updateViewPosition();
					break;

				case MotionEvent.ACTION_UP:
//					float actionUpx2 = event.getX();
//					float actionUpy2 = event.getY();
//					if(Math.abs(actionUpx-actionUpx2) <3||Math.abs(actionUpy-actionUpy2) <3){
//						return true;
//					}
//					
//					if(Math.abs(actionUpx-actionUpx2) >60 || Math.abs(actionUpy-actionUpy2) >60){
//						return true;
//					}
					updateViewPosition();
					mTouchStartX = mTouchStartY = 0;
					break;

				}
				return false;
			}
		});
		createView();
	}
	
	
	
	
	
	Handler handler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			getBackground().setAlpha(70);
		};
	};
	
	
	
	public  void  setChangeBackGroud(){
		getBackground().setAlpha(255);
		handler.removeMessages(1000);
		handler.sendEmptyMessageDelayed(1000, 2000);
	}
	
	
	/**
	 * 创建view
	 */
	private void createView() {
		// 获取WindowManager
		wmParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
		// 该类型提供与用户交互，置于所有应用程序上方，但是在状态栏后面
		// TYPE_TOAST TYPE_SYSTEM_OVERLAY 在其他应用上层 在通知栏下层 位置不能动鸟

		// TYPE_PHONE 在其他应用上层 在通知栏下层

		// TYPE_PRIORITY_PHONE TYPE_SYSTEM_ALERT 在其他应用上层 在通知栏上层 没试出来区别是啥

		// TYPE_SYSTEM_ERROR 最顶层(通过对比360和天天动听歌词得出)

		// 用别的TYPE还出报错... 也希望大家补充一下

		wmParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;// 不接受任何按键事件

		wmParams.gravity = Gravity.LEFT | Gravity.TOP; // 调整悬浮窗口至左上角

		// 以屏幕左上角为原点，设置x、y初始值
		wmParams.x =displayMetrics.widthPixels- displayMetrics.widthPixels/5;
		wmParams.y = 20;
		// 设置悬浮窗口长宽数据
		wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
		wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
		wmParams.format = PixelFormat.RGBA_8888;
		wm.addView(this, wmParams);
		handler.removeMessages(1000);
		handler.sendEmptyMessageDelayed(1000, 2000);
	}

	/**
	 *更新浮动窗口位置参数
	 */
	private void updateViewPosition() {
		wmParams.x = (int) (x - mTouchStartX);
		wmParams.y = (int) (y - mTouchStartY);
		wm.updateViewLayout(this, wmParams);
	}
	

}
